for _ in range(int(input())):
input()
pilha_presentes = {}
for indice, presente in enumerate(list(map(int, input().split(" ")))):
pilha_presentes[presente] = indice
tempo = 0
cont_enviados = 0
maior_enviado = -1
for presente in list(map(int, input().split(" "))):
indice = pilha_presentes[presente]
if indice > maior_enviado:
maior_enviado = indice
tempo += (indice - cont_enviados) * 2 + 1
else:
tempo += 1
cont_enviados += 1
print(tempo)
#include <bits/stdc++.h>
#include <algorithm>
#define vi vector<long long>
#define ll long long int
#define lcm(a, b) (a * b) / __gcd(a, b)
#define sr(a) sort(a.begin(), a.end())
#define fr(i, a, b) for (long long i = a; i < b; i++)
#define srr(a) sort(a.rbegin(), a.rend())
#define mx(a) *max_element(a.begin(), a.end());
#define mn(a) *min_element(a.begin(), a.end());
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define tolow(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define toupp(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define yesOrNo(cond) cout << ((cond) ? "YES\n" : "NO\n");
using namespace std;
const ll P = 1e9 + 7;
const ll N = 1e5 + 2;
bool isPrime(int number)
{
if (number < 2)
return false;
if (number == 2)
return true;
if (number % 2 == 0)
return false;
for (int i = 3; (i * i) <= number; i += 2)
{
if (number % i == 0)
return false;
}
return true;
}
vi ayush(ll n)
{
bool prime[n + 1] = {true};
for (ll i = 2; i <= n; i++)
{
if (prime[i] == true)
{
for (ll j = i * i; j <= n; j++)
{
prime[j] = false;
}
}
}
vi res;
for (ll i = 2; i <= n; i++)
{
if (prime[i] == true)
{
res.push_back(i);
}
}
return res;
}
bool isInt(float k)
{
return (floor(k) == ceil(k)) ? true : false;
}
bool isPow2(ll n)
{
return (n && !(n & (n - 1)));
}
bool isPsquare(ll f)
{
ll r = sqrt(f);
return (r * r == f);
}
bool isValid(string s)
{
int ans = 0;
for (char c : s)
{
if (c == '(')
{
ans++;
}
else
{
if (ans > 0)
{
ans--;
}
else
{
return false;
}
}
}
return true;
}
ll mul(ll x, ll y)
{
return ((x * y) % P); // time complexity O(1)
}
ll fact[N];
void calculate_factorial()
{
fact[0] = 1;
for (ll i = 1; i < N; i++)
{
fact[i] = mul(fact[i - 1], i); // time complexity(O(N))
}
}
ll powrm(ll x, ll y)
{
ll res = 1;
while (y)
{
if (y & 1)
{
res = mul(x, res); // time complexity O(log(Y))
}
y = y >> 1;
x = mul(x, x);
}
return res;
}
ll inv(ll x)
{
return powrm(x, P - 2); // time complexity O(log(Y))
}
ll divm(ll x, ll y)
{
return mul(x, powrm(y, P - 2)); // time complexity O(log(Y))
}
ll ncr(ll n, ll r)
{
if (n >= r)
{
calculate_factorial();
return mul(mul(fact[n], inv(fact[r])), inv(fact[n - r])); // time complexity O(log(Y))
}
else
{
return -1;
}
}
void solve()
{
ll n, m;
cin >> n >> m;
ll p[N];
vi q(m, 0);
fr(i, 0, n)
{
ll k;
cin >> k;
p[k] = i + 1;
}
fr(i, 0, m)
{
cin >> q[i];
}
ll mx = p[q[0]];
ll ans = 2 * (p[q[0]] - 1) + 1;
ll left = 1;
fr(i, 1, m)
{
if (p[q[i]] < mx)
{
ans++;
left++;
}
else
{
ans += 2 * (p[q[i]] - 1 - left) + 1;
left++;
mx = p[q[i]];
}
}
cout << ans << "\n";
}
int main()
{
fast int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
1302. Deepest Leaves Sum | 1209. Remove All Adjacent Duplicates in String II |
994. Rotting Oranges | 983. Minimum Cost For Tickets |
973. K Closest Points to Origin | 969. Pancake Sorting |
967. Numbers With Same Consecutive Differences | 957. Prison Cells After N Days |
946. Validate Stack Sequences | 921. Minimum Add to Make Parentheses Valid |
881. Boats to Save People | 497. Random Point in Non-overlapping Rectangles |
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |